//
//  Transition.swift
//  DYSwiftFramework
//
//  Created by 黄德玉 on 2018/8/16.
//  Copyright © 2018年 none. All rights reserved.
//

import UIKit

class Transition: NSObject,UIViewControllerTransitioningDelegate,UINavigationControllerDelegate {
    
    public var toAnimator:Animator?
    public var backAnimator:Animator?
    public var toInteractor:Interactor?
    public var backInteractor:Interactor?
    
    private var operation :UINavigationControllerOperation = .none
    
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return toAnimator;
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return backAnimator;
    }
    func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if let to = toInteractor {
            return to.canInteractive ? toInteractor : nil
        }
        return nil;
    }
    func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if let back = backInteractor {
            return back.canInteractive ? backInteractor : nil
        }
        return nil;
    }
    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?{
        if operation == .push {
            if let to = toInteractor {
                return to.canInteractive ? toInteractor : nil
            }
             return nil;
        }else if operation == .pop{
            if let back = backInteractor {
                return back.canInteractive ? backInteractor : nil
            }
            return nil;
        }
        return nil;
    }
    
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        self.operation = operation
        if operation == .push {
            return toAnimator;
        }else if operation == .pop{
            return backAnimator
        }
        return nil
    }
    
    
    
    deinit {
        debugPrint("转场控制器释放")
    }
    
}


